﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PragmaTouchUtils;

namespace eBook.WinApp
{
  public partial class ucWebDataProviderOptions : ucOptionEditorBase, IConfigContentEditor
  {

    public ucWebDataProviderOptions()
    {
      InitializeComponent();

      this.ItemClassName = "WebDataProviderOptions";
      this.Caption = "Web data provider";
    }

    protected override void  LoadInitial()
    {
      WebDataProviderOptions options = _currentConfig.GetConfigContentItem(this.ItemClassName) as WebDataProviderOptions;
      if ( options == null )
        throw new Exception(string.Format("Configuration content does not contain {0} item!", this.ItemClassName));

      txtBaseUrl.DataBindTo(options, "BaseUrl", this.ControlValueChanged);
      chkRequiresAuthentication.DataBindTo(options, "RequiresAuthentication", this.ControlValueChanged);
      txtLoginPageUrl.DataBindTo(options, "LoginPageUrl", this.ControlValueChanged);
      txtUsername.DataBindTo(options, "Username", this.ControlValueChanged);
      txtPassword.DataBindTo(options, "Password", this.ControlValueChanged);

      this.RequireAuthentication(chkRequiresAuthentication.Checked);
    }

    private void RequireAuthentication(bool enabled)
    {
      txtLoginPageUrl.Enabled = enabled;
      txtUsername.Enabled = enabled;
      txtPassword.Enabled = enabled;
    }

    private void ControlValueChanged(object sender, EventArgs e)
    {
      this.Modified = true;
      if ( sender == chkRequiresAuthentication )
        this.RequireAuthentication(chkRequiresAuthentication.Checked);
    }

  }
}
